
import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);
Vuex.Store.prototype.http = axios;
import layer from 'vue-layer'
Vuex.Store.prototype.layer = layer(Vue);

var store = new Vuex.Store({
    // 初始值
    state:{
        // 退出登录时用           
        list:false,
        falshTitle:false,
        home:[],
        isLogin:false,
        spin:false,
        word:0,
        news:[],
        reply:{},
    },
    // 修改初始值
    mutations:{
        home1:function(state,value){
            state.home = value;
        },
        isLogin1:function(state,value){
            state.isLogin = value;
        },
        // 退出登录时用
        falshTitle1:function(state,data){
            state.falshTitle = true;
        },
        list1:function(state,value){
            state.list = value;
        },
        word1:function(state,value){
            state.word = value;
        },
        news1:function(state,value){
            state.news = value;
        },
        reply2:function(state,value){
            state.reply = value;
        }
    },
    // mutation只用于同步的数据修改，异步的数据修改（例如网络请求的数据）需要使用actions
    actions:{
        home2:function(state,data){
            this.http.get("/diary/home")
            .then(res=>{
                // console.log(res.data.data);
                state.commit("home1",res.data.data);
            })
        },
        isLogin2:function(state,data){
            this.http.get("/users/isLogin")
            .then(res=>{
                if(res.data.err){
                    state.commit("isLogin1",false);
                }else{
                    state.commit("isLogin1",true);
                }
            })
        },
        // 上传头像
        avatarimg:function(state,data){
            var datas = new FormData();
            datas.append("avatar",data);
            this.http({
                method:"post",
                url:"/users/avatar",
                data:datas,
                headers:{'Content-Type':"multipart/form-data"},
            })
            .then(res=>{
                if(!res.data.err){
                    Vue.prototype.layer.msg("上传成功");
                    window.location.href = "http://localhost:8080/#/new/day/me"
                }
                if(res.data.err==5){
                    Vue.prototype.layer.msg("未登录");
                    window.location.href = "http://localhost:8080"
                }
            })
        },
        delLog:function(state,data){
            this.http.get("/reply/del",{params:{id:data}})
            .then(res=>{
                console.log(res.data);
                if(!res.data.err){
                    window.location.href = "http://localhost:8080/#/new/day/me"               
                }
            })
        },
        // 查看是否有消息
        word:function(state,data){
            this.http.get("/reply/word")
            .then(res=>{
                if(res.data.err){
                    state.commit("word1",0)
                }else{
                    state.commit("word1",res.data.data.length+0)
                }
            })
        },
        // 进入消息页面发的请求
        canWord:function(state,data){
            this.http.get("/reply/canword")
            .then(res=>{
                if(res.data.err==5){
                    this.layer.alert("请先登陆")
                    return;
                }
                state.commit("word1",0)
                state.commit("news1",res.data.data.reverse());
                
            })
        },
        // 删除点击消息
        delWord:function(state,data){
            this.http.get("/reply/delword",{params:{id:data}})
            .then(res=>{
                this.layer.msg(res.data.msg)
                state.dispatch("canWord");
            })
        },
        // 进入回复的回复
        replyID:function(state,data){
            this.http.post("/reply/reply/replys",data)
            .then(res=>{
                if(res.data.err){
                    window.location.href="#/reply/"+data.logID;
                    this.layer.msg(res.data.msg);
                }else{
                    window.location.href="#/reply/"+data.logID+'/'+data.repID;  
                    state.commit("reply2",res.data.data);                  
                }
            })

           
        },
    }
});
export default store;